using SS.dir;
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace SS
{
	public class frmDataAct : Form
	{
		private frmWaitting objW;
		private Func objFunc = new Func();
		private IContainer components = null;
		private Button btnRestore;
		private Button btnCreate;
		private OpenFileDialog openBackupDialog;
		private SaveFileDialog saveBackupDialog;
		private Button btnClose;
		private GroupBox groupBox3;
		private Button btnFind;
		private Label label1;
		private TextBox txtResName;
		private Button btnChooiceDir;
		private Label lblCurrDatabase;
		private TextBox txtPathNew;
		private Label label2;
		private FolderBrowserDialog folderBrowserDialog1;
		private TextBox txtRestoreFile;
		private Label label3;
		public frmDataAct()
		{
			this.InitializeComponent();
		}
		private void showWatting()
		{
			this.objW = new frmWaitting();
			this.objW.ShowDialog();
			Thread.Sleep(40);
		}
		private void frmDataAct_Load(object sender, EventArgs e)
		{
			this.lblCurrDatabase.Text = ConfigurationSettings.AppSettings["DataBase"].ToString();
		}
		private void btnCreate_Click(object sender, EventArgs e)
		{
			try
			{
				this.saveBackupDialog.FileName = string.Concat(new string[]
				{
					this.lblCurrDatabase.Text,
					"-",
					DateTime.Now.Day.ToString(),
					"-",
					DateTime.Now.Month.ToString(),
					"-",
					DateTime.Now.Year.ToString()
				});
				if (this.saveBackupDialog.ShowDialog() == DialogResult.OK)
				{
					SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
					sqlConnection.Open();
					SqlCommand sqlCommand = new SqlCommand("doBackup", sqlConnection);
					sqlCommand.CommandType = CommandType.StoredProcedure;
					sqlCommand.Parameters.Add("databaseName", SqlDbType.VarChar, 250);
					sqlCommand.Parameters.Add("databasePath", SqlDbType.VarChar, 500);
					sqlCommand.Parameters["databaseName"].Value = this.lblCurrDatabase.Text;
					sqlCommand.Parameters["databasePath"].Value = this.saveBackupDialog.FileName;
					try
					{
						sqlCommand.ExecuteNonQuery();
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message);
					}
					sqlConnection.Close();
					sqlConnection.Dispose();
					MessageBox.Show("Quá trình sao lưu dữ liệu đã hoàn thành", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
			}
			catch
			{
				MessageBox.Show("Quá trình sao lưu dữ liệu không hoàn thanh.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}
		private void btnRestore_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.txtRestoreFile.Text.Trim() == "" || this.txtResName.Text.Trim() == "" || this.txtPathNew.Text.Trim() == "")
				{
					MessageBox.Show("Chưa đủ thông tin để thực hiện quá trình phục hồi dữ liệu !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
				else
				{
					SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
					sqlConnection.Open();
					SqlCommand sqlCommand = new SqlCommand("doRestore", sqlConnection);
					sqlCommand.CommandType = CommandType.StoredProcedure;
					sqlCommand.Parameters.Add("databaseNewName", SqlDbType.VarChar, 250);
					sqlCommand.Parameters.Add("databasePath", SqlDbType.VarChar, 500);
					sqlCommand.Parameters.Add("databaseNewDatePath", SqlDbType.VarChar, 500);
					sqlCommand.Parameters.Add("databaseNewLogPath", SqlDbType.VarChar, 500);
					sqlCommand.Parameters["databaseNewName"].Value = this.txtResName.Text;
					sqlCommand.Parameters["databasePath"].Value = this.txtRestoreFile.Text;
					sqlCommand.Parameters["databaseNewDatePath"].Value = this.txtPathNew.Text + "\\" + this.txtResName.Text + ".MDF";
					sqlCommand.Parameters["databaseNewLogPath"].Value = this.txtPathNew.Text + "\\" + this.txtResName.Text + ".LDF";
					try
					{
						sqlCommand.ExecuteNonQuery();
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message);
					}
					sqlConnection.Close();
					sqlConnection.Dispose();
					MessageBox.Show("Việc phục hồi dữ liệu đã thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
			}
			catch
			{
				MessageBox.Show("Việc phục hồi dữ liệu không thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}
		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void btnFind_Click(object sender, EventArgs e)
		{
			if (this.openBackupDialog.ShowDialog() == DialogResult.OK)
			{
				this.txtRestoreFile.Text = this.openBackupDialog.FileName;
				this.btnRestore.Enabled = true;
			}
			else
			{
				this.btnRestore.Enabled = false;
			}
		}
		private void txtResName_TextChanged(object sender, EventArgs e)
		{
			if (this.txtResName.Text.Trim() == "")
			{
				this.btnRestore.Enabled = false;
			}
			else
			{
				this.btnRestore.Enabled = true;
			}
		}
		private void frmDataAct_Shown(object sender, EventArgs e)
		{
		}
		private void btnChooiceDir_Click(object sender, EventArgs e)
		{
			this.txtPathNew.Text = "";
			this.folderBrowserDialog1.ShowDialog();
			if (this.folderBrowserDialog1.SelectedPath != "")
			{
				this.txtPathNew.Text = this.folderBrowserDialog1.SelectedPath;
			}
		}
		private void frmDataAct_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData == Keys.Escape)
			{
				base.Close();
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(frmDataAct));
			this.btnRestore = new Button();
			this.btnCreate = new Button();
			this.openBackupDialog = new OpenFileDialog();
			this.saveBackupDialog = new SaveFileDialog();
			this.btnClose = new Button();
			this.groupBox3 = new GroupBox();
			this.txtRestoreFile = new TextBox();
			this.label3 = new Label();
			this.txtPathNew = new TextBox();
			this.label2 = new Label();
			this.btnChooiceDir = new Button();
			this.txtResName = new TextBox();
			this.btnFind = new Button();
			this.label1 = new Label();
			this.lblCurrDatabase = new Label();
			this.folderBrowserDialog1 = new FolderBrowserDialog();
			this.groupBox3.SuspendLayout();
			base.SuspendLayout();
			this.btnRestore.Cursor = Cursors.Hand;
			this.btnRestore.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnRestore.Location = new Point(263, 107);
			this.btnRestore.Name = "btnRestore";
			this.btnRestore.Size = new Size(119, 27);
			this.btnRestore.TabIndex = 2;
			this.btnRestore.Text = "Restore Backup";
			this.btnRestore.UseVisualStyleBackColor = true;
			this.btnRestore.Click += new EventHandler(this.btnRestore_Click);
			this.btnCreate.Cursor = Cursors.Hand;
			this.btnCreate.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnCreate.Location = new Point(290, 12);
			this.btnCreate.Name = "btnCreate";
			this.btnCreate.Size = new Size(107, 27);
			this.btnCreate.TabIndex = 12;
			this.btnCreate.Text = "Tạo bản sao lưu";
			this.btnCreate.UseVisualStyleBackColor = true;
			this.btnCreate.Click += new EventHandler(this.btnCreate_Click);
			this.openBackupDialog.FileName = "TVSGPP.bak";
			this.openBackupDialog.Filter = "Backup File|*.bak";
			this.saveBackupDialog.FileName = "TVSGPP.bak";
			this.saveBackupDialog.Filter = "Backup File|*.bak";
			this.btnClose.Cursor = Cursors.Hand;
			this.btnClose.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnClose.Location = new Point(303, 193);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new Size(94, 28);
			this.btnClose.TabIndex = 12;
			this.btnClose.Text = "Thoát [ ESC ]";
			this.btnClose.UseVisualStyleBackColor = true;
			this.btnClose.Click += new EventHandler(this.btnClose_Click);
			this.groupBox3.Controls.Add(this.txtRestoreFile);
			this.groupBox3.Controls.Add(this.label3);
			this.groupBox3.Controls.Add(this.txtPathNew);
			this.groupBox3.Controls.Add(this.label2);
			this.groupBox3.Controls.Add(this.btnChooiceDir);
			this.groupBox3.Controls.Add(this.txtResName);
			this.groupBox3.Controls.Add(this.btnFind);
			this.groupBox3.Controls.Add(this.label1);
			this.groupBox3.Controls.Add(this.btnRestore);
			this.groupBox3.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.groupBox3.Location = new Point(7, 45);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new Size(390, 142);
			this.groupBox3.TabIndex = 13;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Phục hồi dữ liệu";
			this.txtRestoreFile.BackColor = Color.White;
			this.txtRestoreFile.Location = new Point(138, 21);
			this.txtRestoreFile.Name = "txtRestoreFile";
			this.txtRestoreFile.ReadOnly = true;
			this.txtRestoreFile.Size = new Size(244, 21);
			this.txtRestoreFile.TabIndex = 18;
			this.label3.AutoSize = true;
			this.label3.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 163);
			this.label3.Location = new Point(8, 24);
			this.label3.Name = "label3";
			this.label3.Size = new Size(126, 15);
			this.label3.TabIndex = 17;
			this.label3.Text = "Dữ liệu cần phục hồi :";
			this.txtPathNew.BackColor = Color.White;
			this.txtPathNew.Location = new Point(138, 75);
			this.txtPathNew.Name = "txtPathNew";
			this.txtPathNew.ReadOnly = true;
			this.txtPathNew.Size = new Size(244, 21);
			this.txtPathNew.TabIndex = 16;
			this.label2.AutoSize = true;
			this.label2.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 163);
			this.label2.Location = new Point(51, 78);
			this.label2.Name = "label2";
			this.label2.Size = new Size(83, 15);
			this.label2.TabIndex = 15;
			this.label2.Text = "Nơi phục hồi :";
			this.btnChooiceDir.Cursor = Cursors.Hand;
			this.btnChooiceDir.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnChooiceDir.Location = new Point(138, 107);
			this.btnChooiceDir.Name = "btnChooiceDir";
			this.btnChooiceDir.Size = new Size(119, 27);
			this.btnChooiceDir.TabIndex = 14;
			this.btnChooiceDir.Text = "Chọn nơi phục hồi";
			this.btnChooiceDir.UseVisualStyleBackColor = true;
			this.btnChooiceDir.Click += new EventHandler(this.btnChooiceDir_Click);
			this.txtResName.Location = new Point(138, 48);
			this.txtResName.Name = "txtResName";
			this.txtResName.Size = new Size(244, 21);
			this.txtResName.TabIndex = 13;
			this.txtResName.TextChanged += new EventHandler(this.txtResName_TextChanged);
			this.btnFind.Cursor = Cursors.Hand;
			this.btnFind.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnFind.Location = new Point(25, 107);
			this.btnFind.Name = "btnFind";
			this.btnFind.Size = new Size(107, 27);
			this.btnFind.TabIndex = 12;
			this.btnFind.Text = "Chọn dữ liệu";
			this.btnFind.UseVisualStyleBackColor = true;
			this.btnFind.Click += new EventHandler(this.btnFind_Click);
			this.label1.AutoSize = true;
			this.label1.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 163);
			this.label1.Location = new Point(9, 51);
			this.label1.Name = "label1";
			this.label1.Size = new Size(125, 15);
			this.label1.TabIndex = 4;
			this.label1.Text = "Tên dữ liệu phục hồi :";
			this.lblCurrDatabase.BackColor = Color.FromArgb(255, 255, 192);
			this.lblCurrDatabase.BorderStyle = BorderStyle.Fixed3D;
			this.lblCurrDatabase.Location = new Point(7, 14);
			this.lblCurrDatabase.Name = "lblCurrDatabase";
			this.lblCurrDatabase.Size = new Size(282, 24);
			this.lblCurrDatabase.TabIndex = 15;
			this.lblCurrDatabase.TextAlign = ContentAlignment.MiddleCenter;
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(404, 228);
			base.Controls.Add(this.lblCurrDatabase);
			base.Controls.Add(this.btnCreate);
			base.Controls.Add(this.groupBox3);
			base.Controls.Add(this.btnClose);
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 163);
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			base.MaximizeBox = false;
			this.MaximumSize = new Size(420, 262);
			this.MinimumSize = new Size(420, 262);
			base.Name = "frmDataAct";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = " ..: SAO LUU - PHUC HOI DU LIEU";
			base.Load += new EventHandler(this.frmDataAct_Load);
			base.Shown += new EventHandler(this.frmDataAct_Shown);
			base.KeyDown += new KeyEventHandler(this.frmDataAct_KeyDown);
			this.groupBox3.ResumeLayout(false);
			this.groupBox3.PerformLayout();
			base.ResumeLayout(false);
		}
	}
}
